{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸识别\n",
    "\n",
    "百度百科:人脸识别，是基于人的脸部特征信息进行身份识别的一种生物识别技术。用摄像机或摄像头采集含有人脸的图像或视频流，并自动在图像中检测和跟踪人脸，进而对检测到的人脸进行脸部识别的一系列相关技术，通常也叫做人像识别、面部识别。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 深度卷积神经网络——CNN\n",
    "\n",
    "图像识别一般采用，深度卷积神经网络——CNN。\n",
    "\n",
    "关于CNN，可以自行百度。下面我给出几个参考连接：\n",
    "\n",
    "(1)百度百科:https://baike.baidu.com/item/%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/17541100?fr=aladdin\n",
    "\n",
    "(2)卷积神经网络(CNN)模型结构:https://www.cnblogs.com/pinard/p/6483207.html\n",
    "\n",
    "(3)从此明白了卷积神经网络（CNN）:https://www.jianshu.com/p/c0215d26d20a\n",
    "\n",
    "(4)卷积神经网络简介:https://zhuanlan.zhihu.com/p/25249694"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始我们的实验\n",
    "\n",
    "## (1)登录百度AI开发平台，选择“人脸识别”，使用“创建应用”建立一个人脸识别应用。\n",
    "\n",
    "获取应用的AppID、API Key、Secret Key，注意:Secret Key要先选择显示，然后复制其现实的字符。\n",
    "\n",
    "例如我创建的人脸识别应用获取的AppID、API Key、Secret Key如下：\n",
    "\n",
    "    \"\"\" 公开课的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "    APP_ID = '21105751'\n",
    "    API_KEY = 'fo4RCGuZMOouyY19UTA2U9Gf'\n",
    "    SECRET_KEY = 'MUIfE4YLofYc1pEaX2yBABV6L5TZGpKM'\n",
    "   \n",
    "百度AI开发平台注册链接:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview\n",
    "\n",
    "## (2)熟悉我封装的百度AI人脸识别的模块\n",
    "\n",
    "在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    from baidu_picture import baidu_picture_2_msg\n",
    "封装库提供两个方法：初始化方法，语音识别方法。\n",
    "\n",
    "### <1>初始化方法，需要传入三个参数，就是上步我们创建“人脸识别”应用获取的三个参数\n",
    "参数信息如下：\n",
    "\n",
    "    \"\"\" 公开课的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "    APP_ID = '21105751'\n",
    "    API_KEY = 'fo4RCGuZMOouyY19UTA2U9Gf'\n",
    "    SECRET_KEY = 'MUIfE4YLofYc1pEaX2yBABV6L5TZGpKM'\n",
    "   \n",
    "调用方式：\n",
    "\n",
    "    # 传入百度AI的参数\n",
    "    pic_msg = baidu_picture_2_msg(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "初始化完毕后，获取pic_msg对象，操作该对象就能进行人脸识别。\n",
    "\n",
    "### <2>人脸识别方法\n",
    "这是我封装百度语音识别方法的函数原型：\n",
    "\n",
    "    #从百度AI获取图片分析结果\n",
    "    response = pic_msg.pic_2_msg(baidu_request_url, 'camera_pic.jpg')   \n",
    "    \n",
    "pic_2_msg包含2个参数：\n",
    "\n",
    "    baidu_request_url:百度AI的调用接口，baidu_request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"。\n",
    "    'camera_pic.jpg':进行人脸识别的图片。\n",
    "    \n",
    "返回值:response为人脸识别的信息\n",
    "\n",
    "    response.json()[\"result\"][\"face_num\"]:存储识别出人脸的个数\n",
    "    response.json()[\"result\"][\"face_list\"][i][\"location\"]:第i个人脸的位置信息\n",
    "    response.json()[\"result\"][\"face_list\"][i][\"age\"]:第i个人脸的年龄信息\n",
    "    ...还有不少参数，通过print(response.json())可以发现\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下小派的定位功能，时刻盯着你,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "from playsound import playsound\n",
    "import cv2 as cv\n",
    "import time\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_picture import baidu_picture_2_msg\n",
    "\n",
    "\"\"\" 公开课的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "APP_ID = '20558036'\n",
    "API_KEY = 'u3L19UyWw6Tmx8fvnkqufP4y'\n",
    "SECRET_KEY = '7VjAOyufSVY3IjlM6WV79xrhtivGwO8O'\n",
    "\n",
    "#百度AI的调用url\n",
    "baidu_request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        # 打开摄像头\n",
    "        capture = cv.VideoCapture(0) \n",
    "        \n",
    "        # 传入百度AI的参数\n",
    "        pic_msg = baidu_picture_2_msg(APP_ID, API_KEY, SECRET_KEY)\n",
    "        \n",
    "        picture_time = 0\n",
    "        \n",
    "        while True:\n",
    "            # 一帧一帧读取视频\n",
    "            ret, frame = capture.read()\n",
    "            \n",
    "            #读取当前摄像头大约在10帧每秒\n",
    "            picture_time += 1\n",
    "            if (picture_time % 10) == 1:\n",
    "            \n",
    "                # 写入图片\n",
    "                cv.imwrite('camera_pic.jpg',frame)  \n",
    "\n",
    "                #从百度AI获取图片分析结果\n",
    "                response = pic_msg.pic_2_msg(baidu_request_url, 'camera_pic.jpg')\n",
    "                \n",
    "                #输出所有的分析数据\n",
    "                #print(response.json())\n",
    "                \n",
    "                #获取分析数据是否正确\n",
    "                error_msg = response.json()[\"error_msg\"]\n",
    "                \n",
    "                #获取到正确信息\n",
    "                if error_msg == 'SUCCESS': \n",
    "                    #获取脸的个数\n",
    "                    face_num = response.json()[\"result\"][\"face_num\"]\n",
    "\n",
    "                    #获取人脸位置，画出图框\n",
    "                    for i in range(face_num):\n",
    "                        location = response.json()[\"result\"][\"face_list\"][i][\"location\"]\n",
    "                        left_top = (int(location['left']), int(location['top']))\n",
    "                        right_bottom = (int(left_top[0] + location['width']), int(left_top[1] + location['height']))\n",
    "                        cv.rectangle(frame, left_top, right_bottom, (0,255,0),2)  \n",
    "                    \n",
    "                # 本地显示视频图像\n",
    "                cv.imshow('screen', frame) \n",
    "                cv.waitKey(1)  \n",
    "                    \n",
    "        os.remove('camera_pic.jpg')\n",
    "               \n",
    "    except KeyboardInterrupt:             \n",
    "        # 释放cap,销毁窗口\n",
    "        capture.release()                                   \n",
    "        cv.destroyAllWindows()  \n",
    "        os.remove('camera_pic.jpg')\n",
    "        print(\"任务被终止\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，学习“人脸识别”功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #导入标准库\n",
    "# import sys\n",
    "\n",
    "# import os\n",
    "\n",
    "# from playsound import playsound\n",
    "\n",
    "# import cv2 as cv\n",
    "\n",
    "# import time\n",
    "\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "\n",
    "# from baidu_picture import baidu_picture_2_msg\n",
    "\n",
    "\n",
    "# \"\"\" 公开课的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "# APP_ID = '20558036'\n",
    "\n",
    "# API_KEY = 'u3L19UyWw6Tmx8fvnkqufP4y'\n",
    "\n",
    "# SECRET_KEY = '7VjAOyufSVY3IjlM6WV79xrhtivGwO8O'\n",
    "\n",
    "\n",
    "# #百度AI的调用url\n",
    "# baidu_request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "\n",
    "#     try:\n",
    "\n",
    "#         # 打开摄像头\n",
    "#         capture = cv.VideoCapture(0) \n",
    "\n",
    "        \n",
    "#         # 传入百度AI的参数\n",
    "#         pic_msg = baidu_picture_2_msg(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "        \n",
    "#         picture_time = 0\n",
    "\n",
    "        \n",
    "#         while True:\n",
    "\n",
    "#             # 一帧一帧读取视频\n",
    "#             ret, frame = capture.read()\n",
    "\n",
    "            \n",
    "#             #读取当前摄像头大约在10帧每秒\n",
    "#             picture_time += 1\n",
    "\n",
    "#             if (picture_time % 10) == 1:\n",
    "\n",
    "            \n",
    "#                 # 写入图片\n",
    "#                 cv.imwrite('camera_pic.jpg',frame)  \n",
    "\n",
    "\n",
    "#                 #从百度AI获取图片分析结果\n",
    "#                 response = pic_msg.pic_2_msg(baidu_request_url, 'camera_pic.jpg')\n",
    "\n",
    "                \n",
    "#                 #输出所有的分析数据\n",
    "#                 #print(response.json())\n",
    "                #获取分析数据是否正确\n",
    "#                 error_msg = response.json()[\"error_msg\"]\n",
    "                \n",
    "#                 #获取到正确信息\n",
    "#                 if error_msg == 'SUCCESS': \n",
    "                \n",
    "#                     #获取脸的个数\n",
    "#                     face_num = response.json()[\"result\"][\"face_num\"]\n",
    "\n",
    "\n",
    "#                     #获取人脸位置，画出图框\n",
    "#                     for i in range(face_num):\n",
    "\n",
    "#                         location = response.json()[\"result\"][\"face_list\"][i][\"location\"]\n",
    "\n",
    "#                         left_top = (int(location['left']), int(location['top']))\n",
    "\n",
    "#                         right_bottom = (int(left_top[0] + location['width']), int(left_top[1] + location['height']))\n",
    "\n",
    "#                         cv.rectangle(frame, left_top, right_bottom, (0,255,0),2)  \n",
    "\n",
    "                    \n",
    "#                 # 本地显示视频图像\n",
    "#                 cv.imshow('screen', frame) \n",
    "\n",
    "#                 cv.waitKey(1)  \n",
    "\n",
    "                    \n",
    "#         os.remove('camera_pic.jpg')\n",
    "\n",
    "               \n",
    "#     except KeyboardInterrupt: \n",
    "\n",
    "#         # 释放cap,销毁窗口\n",
    "#         capture.release()  \n",
    "\n",
    "#         cv.destroyAllWindows()  \n",
    "\n",
    "#         os.remove('camera_pic.jpg')\n",
    "\n",
    "#         print(\"任务被终止\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "\n",
    "1、人脸识别信息中包含人脸的年龄、颜值信息，尝试把他们显示出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "from playsound import playsound\n",
    "import cv2 as cv\n",
    "import time\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_picture import baidu_picture_2_msg\n",
    "\n",
    "\"\"\" 公开课的语音合成+人脸识别，可选更改为自己的api接口 \"\"\"\n",
    "APP_ID = '20558036'\n",
    "API_KEY = 'u3L19UyWw6Tmx8fvnkqufP4y'\n",
    "SECRET_KEY = '7VjAOyufSVY3IjlM6WV79xrhtivGwO8O'\n",
    "\n",
    "#百度AI的调用url\n",
    "baidu_request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        # 打开摄像头\n",
    "        capture = cv.VideoCapture(0) \n",
    "        \n",
    "        # 传入百度AI的参数\n",
    "        pic_msg = baidu_picture_2_msg(APP_ID, API_KEY, SECRET_KEY)\n",
    "        \n",
    "        picture_time = 0\n",
    "        \n",
    "        while True:\n",
    "            # 一帧一帧读取视频\n",
    "            ret, frame = capture.read()\n",
    "            \n",
    "            #读取当前摄像头大约在10帧每秒\n",
    "            picture_time += 1\n",
    "            if (picture_time % 10) == 1:\n",
    "            \n",
    "                # 写入图片\n",
    "                cv.imwrite('camera_pic.jpg',frame)  \n",
    "\n",
    "                #从百度AI获取图片分析结果\n",
    "                response = pic_msg.pic_2_msg(baidu_request_url, 'camera_pic.jpg')\n",
    "                \n",
    "                #输出所有的分析数据\n",
    "                #print(response.json())\n",
    "                #获取分析数据是否正确\n",
    "                error_msg = response.json()[\"error_msg\"]\n",
    "                \n",
    "                #获取到正确信息\n",
    "                if error_msg == 'SUCCESS': \n",
    "                    \n",
    "                    #获取脸的个数\n",
    "                    face_num = response.json()[\"result\"][\"face_num\"]\n",
    "                \n",
    "                    #获取人脸位置，画出图框\n",
    "                    for i in range(face_num):\n",
    "                        location = response.json()[\"result\"][\"face_list\"][i][\"location\"]\n",
    "                        left_top = (int(location['left']), int(location['top']))\n",
    "                        right_bottom = (int(left_top[0] + location['width']), int(left_top[1] + location['height']))\n",
    "                        cv.rectangle(frame, left_top, right_bottom, (0,255,0),2)  \n",
    "\n",
    "                        #显示年龄信息\n",
    "                        age = location = response.json()[\"result\"][\"face_list\"][i][\"age\"]\n",
    "                        font = cv.FONT_HERSHEY_SIMPLEX\n",
    "                        cv.putText(frame, str(age), right_bottom, font, 1.2, (0, 255, 0), 2)\n",
    "                    \n",
    "                # 本地显示视频图像\n",
    "                cv.imshow('screen', frame) \n",
    "                cv.waitKey(1)  \n",
    "                    \n",
    "        os.remove('camera_pic.jpg')\n",
    "               \n",
    "    except KeyboardInterrupt:             \n",
    "        # 释放cap,销毁窗口\n",
    "        capture.release()                                   \n",
    "        cv.destroyAllWindows()  \n",
    "        os.remove('camera_pic.jpg')\n",
    "        print(\"任务被终止\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
